package com.example.bookkeeping;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class HistoryAdaptar extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<HistoryBean> historyBeanList;
    Context context;

    public HistoryAdaptar(List<HistoryBean> historyBeanList, Context context) {
        this.historyBeanList = historyBeanList;
        this.context = context;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.hitstory_list_title,parent,false);
        return new TimeLineTitleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HistoryBean historyBean = historyBeanList.get(position);
        if (holder instanceof TimeLineTitleViewHolder){
            ((TimeLineTitleViewHolder) holder).textView.setText(historyBean.getDataStr());
            RecyclerView contentRv = ((TimeLineTitleViewHolder) holder).recyclerView;
            contentRv.setLayoutManager(new LinearLayoutManager(context));
            ContentAdaptar contentAdaptar = new ContentAdaptar(context,historyBean.getContentBeans());
            contentRv.setAdapter(contentAdaptar);
        }
    }

    @Override
    public int getItemCount() {
        return historyBeanList.size();
    }

    class TimeLineTitleViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        RecyclerView recyclerView;
        public TimeLineTitleViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_his_date);
            recyclerView = itemView.findViewById(R.id.rv_content);
        }
    }


    class ContentAdaptar extends RecyclerView.Adapter<ContentAdaptar.InnerHolder>{
        public Context context;
        public List<String> strs;

        public ContentAdaptar(Context context, List<String> strs) {
            this.context = context;
            this.strs = strs;
        }

        @NonNull
        @Override
        public ContentAdaptar.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.history_list_content,parent,false);

            return new InnerHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ContentAdaptar.InnerHolder holder, int position) {
            holder.textView.setText(strs.get(position));
        }

        @Override
        public int getItemCount() {
            return strs.size();
        }

        public class InnerHolder extends RecyclerView.ViewHolder {
            TextView textView;
            public InnerHolder(@NonNull View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_his_content);
            }
        }
    }

}
